package com.raomucang.minist.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author raomucang
 */
@Controller
public class IndexController {

    private ArrayList<Byte> arrayList = new ArrayList<>();

    @RequestMapping("/")
    public String index() {
        FileInputStream fis = null;
        try {
            File file = ResourceUtils.getFile("classpath:templates/train-images.idx3-ubyte");
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            while (fis.read(bytes) != -1) {
                for (int i = 0; i < bytes.length; i++) {
                    arrayList.add(bytes[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "index";
    }

    @ResponseBody
    @RequestMapping("/getData")
    public String[] getData(int imgNumber) {
        int a = 1 / 0;

        int firstIndex = (imgNumber * 784) + 16;
        int endIndex = (imgNumber * 784) + 16 + 784;

        if (endIndex >= arrayList.size()) {
            return null;
        }

        String[] imageNumber = new String[784];
        for (int i = 0, count = firstIndex; count < endIndex; count++, i++) {
            int rgb = 255 - arrayList.get(count) & 0xFF;
            imageNumber[i] = convertRGBToHex(rgb, rgb, rgb);
        }
        return imageNumber;
    }

    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        int maxLength = 2;

        if (hex.length() < maxLength) {
            hex = "0" + hex;
        }
        return hex;
    }

    public String convertRGBToHex(int r, int g, int b) {
        String rFString, rSString, gFString, gSString, bFString, bSString, result;
        int red, green, blue;
        int rred, rgreen, rblue;
        red = r / 16;
        rred = r % 16;
        if (red == 10)
            rFString = "A";
        else if (red == 11)
            rFString = "B";
        else if (red == 12)
            rFString = "C";
        else if (red == 13)
            rFString = "D";
        else if (red == 14)
            rFString = "E";
        else if (red == 15)
            rFString = "F";
        else
            rFString = String.valueOf(red);

        if (rred == 10)
            rSString = "A";
        else if (rred == 11)
            rSString = "B";
        else if (rred == 12)
            rSString = "C";
        else if (rred == 13)
            rSString = "D";
        else if (rred == 14)
            rSString = "E";
        else if (rred == 15)
            rSString = "F";
        else
            rSString = String.valueOf(rred);

        rFString = rFString + rSString;

        green = g / 16;
        rgreen = g % 16;

        if (green == 10)
            gFString = "A";
        else if (green == 11)
            gFString = "B";
        else if (green == 12)
            gFString = "C";
        else if (green == 13)
            gFString = "D";
        else if (green == 14)
            gFString = "E";
        else if (green == 15)
            gFString = "F";
        else
            gFString = String.valueOf(green);

        if (rgreen == 10)
            gSString = "A";
        else if (rgreen == 11)
            gSString = "B";
        else if (rgreen == 12)
            gSString = "C";
        else if (rgreen == 13)
            gSString = "D";
        else if (rgreen == 14)
            gSString = "E";
        else if (rgreen == 15)
            gSString = "F";
        else
            gSString = String.valueOf(rgreen);

        gFString = gFString + gSString;

        blue = b / 16;
        rblue = b % 16;

        if (blue == 10)
            bFString = "A";
        else if (blue == 11)
            bFString = "B";
        else if (blue == 12)
            bFString = "C";
        else if (blue == 13)
            bFString = "D";
        else if (blue == 14)
            bFString = "E";
        else if (blue == 15)
            bFString = "F";
        else
            bFString = String.valueOf(blue);

        if (rblue == 10)
            bSString = "A";
        else if (rblue == 11)
            bSString = "B";
        else if (rblue == 12)
            bSString = "C";
        else if (rblue == 13)
            bSString = "D";
        else if (rblue == 14)
            bSString = "E";
        else if (rblue == 15)
            bSString = "F";
        else
            bSString = String.valueOf(rblue);
        bFString = bFString + bSString;
        result = "#" + rFString + gFString + bFString;
        return result;
    }
}
